package com.yunfei.apiProductKu;

import com.yunfei.apiProductKu.bo.*;
import com.yunfei.apiProductKu.param.*;
import com.yunfei.apiProductKu.result.GongNengQuanXianResult;
import com.yunfei.apiProductKu.result.ProductKuDetailFileResult;
import com.yunfei.apiProductKu.result.ProductKuDetailResult;
import com.yunfei.apiProductKu.result.ProductKuListResult;
import com.yunfei.bo.product.ProductFileBo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.contants.ProductKuContants;
import com.yunfei.contants.UserCenterContants;
import com.yunfei.enums.ProductKuStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisService;
import com.yunfei.mapper.aiiidb.ProductKuDetailFileMapper;
import com.yunfei.mapper.crm.*;
import com.yunfei.procedure.ProcedureResultModel;
import com.yunfei.procedure.ProductCheckProcedure;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Description: 产品库功能权限
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@Api(value = ProductKuContants.GONGNENG_QUANXIAN,tags = ProductKuContants.GONGNENG_QUANXIAN)
@RestController
@RequestMapping("/api/v3/productKu")
public class ProductKuApiController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private GongNengQuanXianMapper gongNengQuanXianMapper;
    @Autowired
    private ProductKuListMapper productKuListMapper;
    @Autowired
    private ProductKuDetailMapper productKuDetailMapper;
    @Autowired
    private YeWuLianXiRenBoMapper yeWuLianXiRenBoMapper;
    @Autowired
    private ShouYiJiBieMapper shouYiJiBieMapper;
    @Autowired
    private FenHongFangShiMapper fenHongFangShiMapper;
    @Autowired
    private LiuZhuanYiJianMapper liuZhuanYiJianMapper;
    @Autowired
    private ProductKuDetailFileMapper productKuDetailFileMapper;
    @Autowired
    private XuanZeChanPinShiDangXingMapper xuanZeChanPinShiDangXingMapper;
    @Autowired
    private Environment env;
    /**
    * @Description:功能权限
    * @Author: zhengyunfei
    * @Date: 2018/10/30
    * @time:9:13
    */
    @PostMapping(value = "/gnqx",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.GONGNENG_QUANXIAN_GET,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = GongNengQuanXianResult.class)
    @ResponseBody
    public Object all(GongNengQuanXianParam param){
       int count=0;
        try{
            String id=param.getId();
            Map<String,Object> queryMap=new HashMap();
            queryMap.put("id",id);
            count=gongNengQuanXianMapper.count(queryMap);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
    /**
     * @Description:产品列表
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/list",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.LIST,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = ProductKuListResult.class)
    @ResponseBody
    public Object list(ProductKuPram param){
        ResultModel resultModel=new ResultModel();
        List<ProductKuListBo> list=null;
        try{
            String userId=param.getUserId();
            if(StringUtil.isNullOrEmpty(userId)||StringUtil.isNullOrEmpty(userId.trim())){
                resultModel.setCode(ProductKuStatus.USER_ID_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.USER_ID_NOTNULL.getMessage());
                return resultModel;
            }
            String cFundName=param.getcFundName();
            String cAuditFlag=param.getcAuditFlag();
            Map<String,Object> queryMap=new HashMap();
            queryMap.put("userId",userId);
            queryMap.put("cFundName",cFundName);
            queryMap.put("cAuditFlag",cAuditFlag);
            list=productKuListMapper.list(queryMap);
            resultModel.setData(list);
            resultModel.setCode(ProductKuStatus.SUCCESS.getCode());
            resultModel.setMessage(ProductKuStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * @Description:产品详情接口
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/detail",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.DETAIL,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = ProductKuDetailResult.class)
    @ResponseBody
    public Object detail(ProductKuProductDetailPram param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        try{
            String cFundCode=param.getcFundCode();
            if(StringUtil.isNullOrEmpty(cFundCode)||StringUtil.isNullOrEmpty(cFundCode.trim())){
                resultModel.setCode(ProductKuStatus.C_FUNDCODE_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.C_FUNDCODE_NOTNULL.getMessage());
                return resultModel;
            }
            Map<String,Object> queryMap=new HashMap();
            queryMap.put("cFundCode",cFundCode.trim());
            ProductKuDetailBo productDetail=productKuDetailMapper.get(queryMap);
            resultMap.put("productDetail",productDetail);
            //业务联系人
            List<YeWuLianXiRenBo> yeWuLianXiRenBoList=yeWuLianXiRenBoMapper.list(queryMap);
            resultMap.put("ywlxrList",yeWuLianXiRenBoList);
            //收益级别
            List<ShouYiJiBieBo> shouYiJiBieBoList=shouYiJiBieMapper.list(queryMap);
            resultMap.put("syjbList",shouYiJiBieBoList);
            //分红方案
            List<FenHongFangShiBo> fenHongFangShiBoList=fenHongFangShiMapper.list(queryMap);
            resultMap.put("fhfaList",fenHongFangShiBoList);
            //流转意见
            List<LiuZhuanYiJianBo> liuZhuanYiJianBoList=liuZhuanYiJianMapper.list(queryMap);
            resultMap.put("lzyjList",liuZhuanYiJianBoList);
            resultModel.setData(resultMap);
            resultModel.setCode(ProductKuStatus.SUCCESS.getCode());
            resultModel.setMessage(ProductKuStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description:驳回/通过
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/shenpi",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.BOHUI_TONGGUO,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = ProcedureResultModel.class)
    @ResponseBody
    public Object bohuiOrTongGuo(ShenPiParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String cFundCode=param.getcFundCode();//产品id
            if(StringUtil.isNullOrEmpty(cFundCode)||StringUtil.isNullOrEmpty(cFundCode.trim())){
                resultModel.setCode(ProductKuStatus.C_FUNDCODE_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.C_FUNDCODE_NOTNULL.getMessage());
                return resultModel;
            }
            String userId=param.getUserId();//理财师id
            if(StringUtil.isNullOrEmpty(userId)||StringUtil.isNullOrEmpty(userId.trim())){
                resultModel.setCode(ProductKuStatus.USER_ID_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.USER_ID_NOTNULL.getMessage());
                return resultModel;
            }
            String userCheckResult=param.getUserCheckResult();//审核意见标识Y:通过 N驳回不能为空
            if(StringUtil.isNullOrEmpty(userCheckResult)||StringUtil.isNullOrEmpty(userCheckResult.trim())){
                resultModel.setCode(ProductKuStatus.USER_CHECK_RESULT_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.USER_CHECK_RESULT_NOTNULL.getMessage());
                return resultModel;
            }else if(!"Y".equals(userCheckResult)&&!"N".equals(userCheckResult)){
                resultModel.setCode(ProductKuStatus.PARAM_ERROR.getCode());
                resultModel.setMessage(ProductKuStatus.PARAM_ERROR.getMessage());
                return resultModel;
            }
            String userCheckMemo=param.getUserCheckMemo();//审核意见
            if(StringUtil.isNullOrEmpty(userCheckResult)||StringUtil.isNullOrEmpty(userCheckResult.trim())){
                resultModel.setCode(ProductKuStatus.USER_CHECK_MEMO_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.USER_CHECK_MEMO_NOTNULL.getMessage());
                return resultModel;
            }
            String jdbcUrl=env.getProperty(ApplicationConstant.CRM_JDBC_URL);
            String driveClassName=env.getProperty(ApplicationConstant.CRM_driverClassName);
            String jdbcUserName=env.getProperty(ApplicationConstant.CRM_USERNAME);
            String jdbcPassword=env.getProperty(ApplicationConstant.CRM_PASSWORD);
            ProcedureResultModel procedureResultModel= new ProductCheckProcedure().callProcedure(jdbcUrl,driveClassName,jdbcUserName,jdbcPassword,userId,userCheckResult,userCheckMemo,cFundCode);
            resultModel.setData(procedureResultModel);
            resultModel.setCode(ProductKuStatus.SUCCESS.getCode());
            resultModel.setMessage(ProductKuStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    @GetMapping("/files")
    @ApiOperation(value = "产品附件",notes = "产品库->产品详情->产品附件",position = 1,response = ProductKuDetailFileResult.class)
    public Object get(ProductFuJianPram param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String cFundCode=param.getcFundCode();
            if(StringUtil.isNullOrEmpty(cFundCode)||StringUtil.isNullOrEmpty(cFundCode.trim())){
                resultModel.setCode(ProductKuStatus.C_FUNDCODE_NOTNULL.getCode());
                resultModel.setMessage(ProductKuStatus.C_FUNDCODE_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("cFundCode",cFundCode.trim());
            List<ProductFileBo> productFileList= ( List<ProductFileBo> ) productKuDetailFileMapper.findProductFileList(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(productFileList);
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * @Description:产品审批->选择产品适当性
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/xuanZeChanPinShiDangXing",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.XUANZE_CHANPIN_SHIDANGXING,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = XuanZeChanPinShiDangXingBo.class)
    @ResponseBody
    public Object xuanZeChanPinShiDangXing(){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        try{
            Map<String,Object> queryMap=new HashMap();
            //选择产品适当性
            List<XuanZeChanPinShiDangXingBo> liuZhuanYiJianBoList=xuanZeChanPinShiDangXingMapper.list(queryMap);
            resultMap.put("list",liuZhuanYiJianBoList);
            resultModel.setData(resultMap);
            resultModel.setCode(ProductKuStatus.SUCCESS.getCode());
            resultModel.setMessage(ProductKuStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * @Description:产品审批->选择产品适当性-提交
     * @Author: zhengyunfei
     * @Date: 2018/10/30
     * @time:9:13
     */
    @PostMapping(value = "/xuanZeChanPinShiDangXingTiJiao",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = ProductKuContants.XUANZE_CHANPIN_SHIDANGXING_TIJIAO,notes = UserCenterContants.AUTHOR_NOTE,position = 1,response = ResultModel.class)
    @ResponseBody
    public Object xuanZeChanPinShiDangXingSubmit(XuanZeChanPinShiDangXingTiJiaoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        try{
            Map<String,Object> queryMap=new HashMap();
            String insetCustLvl=param.getInsetCustLvl();
            String cCaption=param.getcCaption();
            String  cFundCode=param.getcFundCode();
            if(StringUtil.isNullOrEmpty(insetCustLvl)){
                resultModel.setCode(ProductKuStatus.insetCustLvl_notnull.getCode());
                resultModel.setMessage(ProductKuStatus.insetCustLvl_notnull.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(cCaption)){
                resultModel.setCode(ProductKuStatus.cCaption_notnull.getCode());
                resultModel.setMessage(ProductKuStatus.cCaption_notnull.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(cFundCode)){
                resultModel.setCode(ProductKuStatus.cFundCode_notnull.getCode());
                resultModel.setMessage(ProductKuStatus.cFundCode_notnull.getMessage());
                return resultModel;
            }
            queryMap.put("insetCustLvl",insetCustLvl);
            queryMap.put("cCaption",cCaption);
            queryMap.put("cFundCode",cFundCode);
            //选择产品适当性提交
           xuanZeChanPinShiDangXingMapper.update(queryMap);
            resultModel.setData("数据提交成功");
            resultModel.setCode(ProductKuStatus.SUCCESS.getCode());
            resultModel.setMessage(ProductKuStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

}
